﻿using System;
using Aierp.Repository;
using Aierp.Repository.Implements;
using Aierp.SystemEntities.System;
using Aierp.SystemRepository.Implements;
using Aierp.Utility.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Web;
using NLog.Extensions.Logging;

namespace Aierp
{
    public class Startup
    {
        public Startup(IConfiguration configuration, IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
             .SetBasePath(env.ContentRootPath)
             .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
             .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
             .AddJsonFile("appconfig.json", optional: true, reloadOnChange: true)
             .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSession(o =>
            {
                o.IdleTimeout = TimeSpan.FromSeconds(60 * 60 * 24);
            });
            services.AddMemoryCache();//缓存
            services.AddMvc();
            services.AddRouting();
           
            services.AddScoped<IUserRepository, UserRepository>();//用户
            services.AddScoped<Repository.IRepository<Sys_Roles>, RolesRepository>();//角色
            services.AddScoped<Repository.IRepository<Sys_UserRoles>, UserRolesRepository>();//用户角色
            services.AddScoped<NavigationRepository>();//导航菜单
            services.AddScoped<RoleNavBtnsRepository>();//

            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped<SysVisitor>();//当前用户
            services.AddScoped<DicCategoryRepository>();//词典分类
            services.AddScoped<DicsRepository>();//词典
            services.AddScoped<ButtonsRepository>();//按钮
            services.AddScoped<NavButtonsRepository>();//菜单按钮关系

            //services.AddScoped<SysVisitorCustomer>();
            services.AddScoped<Utility.Mail.SendClound>(); //邮件
            services.AddScoped<CustomersRepository>();//前台注册用户

            //读取自定义配置文件
            services.AddOptions();
            services.Configure<AppConfig>(Configuration.GetSection("AppConfig"));
            //全局设置允许上传的文件大小
            //services.Configure<FormOptions>(options =>
            //{
            //    options.ValueLengthLimit = 60000000;
            //});
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            //日志配置
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();
            loggerFactory.AddNLog();
            app.AddNLogWeb();
            loggerFactory.ConfigureNLog("nlog.config");
            //NLogRecord nl = new NLogRecord();
            //nl.Run();
            //日志配置结束
            app.UseSession();
            app.UseMvc();

            //指定默认文件
            DefaultFilesOptions options = new DefaultFilesOptions();
            options.DefaultFileNames.Clear();
            options.DefaultFileNames.Add("Login.html");
            app.UseDefaultFiles(options);

            app.UseStaticFiles();

            //处理不同类型的文件扩展名
            //var contentTypeProvider = new FileExtensionContentTypeProvider();

            //contentTypeProvider.Mappings.Add(".obj", "application/obj");
            //contentTypeProvider.Mappings.Add(".mtl", "application/mtl");
            //app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = contentTypeProvider });
        }
    }
}
